﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Answer
{
    class XMLReader
    {
        XmlTextReader reader;
        private string m_lastSection = null;
        private string m_lastFileName = null;

        private int sectionId = 1;
        
        public void Open(FileInfo fi)
        {

            reader = new XmlTextReader(fi.FullName);
            reader.WhitespaceHandling = WhitespaceHandling.None;
            reader.Read();
            m_lastFileName = fi.Name;
        }


        
        public Section CreateSection()
        {

            XmlTextReader subreader = reader;
            Section sec = null;
            string message = null;
            while (subreader.Read())
            {
                switch (subreader.NodeType)
                {
                    case XmlNodeType.Element: // Der Knoten ist ein Element.
                        switch (subreader.Name)
                        {
                            case "Message":
                                while (subreader.Read())
                                {
                                    if (subreader.NodeType == XmlNodeType.Text)
                                        message = subreader.Value;
                                    else
                                        break;
                                }
                                if (m_lastSection.ToUpper().Equals("DEFAULT"))
                                    sec = new Section(m_lastSection, m_lastFileName, message, -1);
                                else
                                {
                                    sec = new Section(m_lastSection, m_lastFileName, message, sectionId);
                                    sectionId += 1;
                                }
                                break;
                            case "AnswerList":
                                while (subreader.Read())
                                {
                                    if (subreader.NodeType == XmlNodeType.EndElement && subreader.Name.Equals("AnswerList"))
                                        break;
                                    else
                                    {
                                        if (subreader.NodeType == XmlNodeType.Element && subreader.Name.Equals("Answer"))
                                        {
                                            int level = 0;
                                            bool ignore = false;
                                            bool invite = false;

                                            while (subreader.MoveToNextAttribute())
                                            {
                                                if (subreader.Name == "level")
                                                    level = Convert.ToInt32(subreader.Value);

                                                if (subreader.Name == "ignore")
                                                {
                                                    if (subreader.Value.ToUpper().Equals("TRUE"))
                                                    {
                                                        ignore = true;
                                                    }
                                                    else
                                                    {
                                                        ignore = false;
                                                    }
                                                }

                                                if (subreader.Name == "acceptinvite")
                                                {
                                                    if (subreader.Value.ToUpper().Equals("TRUE"))
                                                    {
                                                        invite = true;
                                                    }
                                                    else
                                                    {
                                                        invite = false;
                                                    }
                                                }

                                            }
                                            while (subreader.Read())
                                            {
                                                if (subreader.NodeType == XmlNodeType.EndElement && subreader.Name.Equals("Answer"))
                                                    break;
                                                else
                                                {
                                                    if (subreader.NodeType == XmlNodeType.Element && subreader.Name.Equals("WriteBackMessage"))
                                                    {
                                                        int delay = 0;
                                                        while (subreader.MoveToNextAttribute())
                                                        {
                                                           
                                                            if (subreader.Name == "delay")
                                                                delay = Convert.ToInt32(subreader.Value);

                                                        }

                                                        while (subreader.Read())
                                                        {
                                                            if (subreader.NodeType == XmlNodeType.Text)
                                                                sec.AddAnswer(new answer(level, subreader.Value, delay, invite, ignore));
                                                            else
                                                                break;
                                                        }
                                                        

                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                break;
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if(subreader.Name.Equals("Section"))
                            return sec;
                        break;
                }

            }

            return sec;


        }

        public Boolean ContainsSection()
        {
            XmlTextReader subreader = reader;
            Boolean ret = false;
            m_lastSection = null;
            while (subreader.Read())
            {
                if (subreader.NodeType == XmlNodeType.Element)
                {
                    if (subreader.IsStartElement())
                    {
                        if (subreader.Name.Equals("Section"))
                        {

                            while (subreader.MoveToNextAttribute())
                            {
                                if (subreader.Name == "name")
                                   m_lastSection = subreader.Value;

                            }
                            ret = true;
                            reader = subreader;
                            break;
                        }
                    }
                }
            }

            return ret;

        }
        public string getLastSectionName()
        {
            return this.m_lastSection;
        }

        public void Close()
        {
            reader.Close();
        }
    }
}
